package neuralNetworks;

public class NetDiff {

	public static double computeDiff(NeuralNetwork<? extends Neuron> nn1, NeuralNetwork<? extends Neuron> nn2) {
		if (nn1.layer.size() != nn2.layer.size()) {
			System.err.println("Wrong Layer size!");
			return -1;
		}
		double diff = 0;
		for (int l = 0; l < nn1.layer.size(); l++) {
			Layer<? extends Neuron> l1 = nn1.layer.get(l);
			Layer<? extends Neuron> l2 = nn2.layer.get(l);
			if (l1.neurons.size() != l2.neurons.size()) {
				System.err.println("Wrong neurons size!");
				return -1;
			}
			for (int n = 0; n < l1.neurons.size(); n++) {
				Neuron n1 = l1.neurons.get(n);
				Neuron n2 = l2.neurons.get(n);
				if (n1.sendConns.size() != n2.sendConns.size()) {
					System.err.println("Wrong send conns size!");
					return -1;
				}
				diff += Math.abs(n1.bias - n2.bias);
				for (int c = 0; c < n1.sendConns.size(); c++) {
					Connection<? extends Neuron> c1 = n1.sendConns.get(c);
					Connection<? extends Neuron> c2 = n2.sendConns.get(c);
					diff += Math.abs(c1.weight - c2.weight);
				}
			}
		}

		return diff;
	}
}
